import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
from sqlalchemy import create_engine
import matplotlib.pyplot as plt
import seaborn as sns

# 数据库配置
db_config = {
    'host': '111.231.14.211',
    'user': 'tushare',
    'password': 'root',
    'database': 'tushare',
    'port': 13307,
    'charset': 'utf8mb4'
}

# 创建数据库引擎
engine = create_engine(
    f"mysql+pymysql://{db_config['user']}:{db_config['password']}@{db_config['host']}:{db_config['port']}/{db_config['database']}?charset=utf8mb4"
)

# SQL 查询
query = """
SELECT d.*, m.net_mf_vol , m.sell_elg_vol , m.buy_elg_vol, m.sell_lg_vol, m.buy_lg_vol , i.closes as i_closes, i.vol as i_vol
FROM date_1 d
JOIN moneyflows m ON d.ts_code = m.ts_code AND d.trade_date = m.trade_date
LEFT JOIN index_daily i ON d.trade_date = i.trade_date and i.ts_code='000001.SH'
WHERE d.trade_date BETWEEN '2023-01-01' AND '2023-12-31' AND d.ts_code='000001.SZ'
"""

# 使用 SQLAlchemy 引擎执行查询并分块读取数据
chunk_size = 10000
df_chunks = pd.read_sql_query(query, engine, chunksize=chunk_size)
df1 = pd.concat(df_chunks, ignore_index=True)

print(df1.head())

# 计算 zd_close 和 hz_close 列
df1['zd_close'] = round((df1['closes'] - df1['closes'].shift(1)) / df1['closes'].shift(1), 2)
df1['hz_close'] = round((df1['i_closes'] - df1['i_closes'].shift(1)) / df1['i_closes'].shift(1), 2)
df1['hz_vol'] = round((df1['i_vol'].shift(1) - df1['i_vol'].shift(2)) / df1['i_vol'].shift(2), 2)

# 删除包含 NaN 值的行
df1 = df1.dropna(subset=['zd_close', 'hz_close', 'hz_vol'])

# 获取数值类型的列，并去除指定的列
numeric_cols = df1.select_dtypes(include=['number']).columns.tolist()
excluded = ['zd_close', 'ts_code', 'trade_date', 'id', 'pre_closes', 'changes', 'pct_chg', 'opens', 'high', 'low', 'closes',
            'vol', 'buy_elg_vol', 'i_closes', 'i_vol', 'sell_lg_vol']
predictors = [col for col in numeric_cols if col not in excluded]

# 构建回归公式
formula = 'zd_close ~ ' + ' + '.join(predictors)
results1 = smf.ols(formula, data=df1).fit()

# 输出回归结果摘要
print(results1.summary())

# 绘制拟合值与观测值的关系图
plt.figure(figsize=(10, 6))
plt.scatter(results1.fittedvalues, df1['zd_close'])
plt.title('Fitted Values vs. Observed Values')
plt.xlabel('Fitted Values')
plt.ylabel('Observed Values')
plt.show()

# 特征相关性分析
features = df1[['amount', 'net_mf_vol', 'sell_elg_vol', 'buy_lg_vol', 'hz_close', 'hz_vol']]
correlation_matrix = features.corr()

# 绘制热力图
plt.figure(figsize=(10, 6))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', cbar=True, fmt='.2f', linewidths=.5)
plt.title('Correlation Matrix between Features')
plt.show()